import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

import org.jbox2d.common.Color3f;
import org.jbox2d.common.Transform;
import org.jbox2d.common.Vec2;


public class GeometricDraw {
	
	private float pixelsPerMeter = 20;
	//private Vec2 screen = new Vec2();
	
	private Component comp;
	
	public GeometricDraw(Component comp) {
		this.comp = comp;
	}
	
	public Graphics2D getGraphics() {
		return (Graphics2D) comp.getGraphics();
	}
	
	public Color getColor(Color3f color) {
		return new Color(color.x, color.y, color.z);
	}
	
	public void circle(Vec2 center, float radius, Color3f color, Color3f fill) {
		center = getScreenVector(center);
		radius = getScreenDistance(radius);
		
		Graphics2D g = getGraphics();
		Ellipse2D circle = new Ellipse2D.Float(center.x - radius, center.y - radius, 2 * radius, 2 * radius);
		
		if(fill != null) {
			g.setColor(getColor(fill));
			g.fill(circle);
		}
		
		g.setColor(getColor(color));
		g.draw(circle);
	}
	
	public void point(Vec2 position, float screenRadius, Color3f color) {
		position = getScreenVector(position);
		circle(position, getWorldDistance(screenRadius), color, color);
	}
	
	public void segment(Vec2 p1, Vec2 p2, Color3f color) {
		p1 = getScreenVector(p1);
		p2 = getScreenVector(p2);
		
		Graphics2D g = getGraphics();
		Line2D line = new Line2D.Float(p1.x, p1.y, p2.x, p2.y);
		
		g.setColor(getColor(color));
		g.draw(line);
	}
	
	/*public void solidCircle(Vec2 center, float radius, Vec2 axis, Color3f color) {
		center.m
	}*/
	
	public void polygon(Vec2[] vertices, int length, Color3f color, Color3f fill) {
		Graphics2D g = getGraphics();
		//int length = vertices.length;
		int[] xPoints = new int[length];
		int[] yPoints = new int[length];
		
		for(int i = 0; i < length; i++) {
			Vec2 v = getScreenVector(vertices[i]);
			System.out.println("-- " + v);
			xPoints[i] = (int) v.x;
			yPoints[i] = (int) v.y;
		}
		
		if(fill != null) {
			g.setColor(getColor(fill));
			g.fillPolygon(xPoints, yPoints, length);
		}
		
		g.setColor(getColor(color));
		g.drawPolygon(xPoints, yPoints, length);
	}
	
	public void transform(Transform transform) {
		System.out.println("--> Transformation " + transform);
	}
	
	public void string(float x, float y, String s, Color3f color) {
		Graphics2D g = getGraphics();
		
		g.setColor(getColor(color));
		g.drawString(s, x, y);
	}
	
	private Vec2 getScreenVector(Vec2 worldVector) {
		return new Vec2(worldVector.x * pixelsPerMeter, worldVector.y * pixelsPerMeter);
	}
	
	private float getScreenDistance(float worldDistance) {
		return worldDistance * pixelsPerMeter;
	}
	
	private Vec2 getWorldVector(Vec2 screenVector) {
		return new Vec2(screenVector.x / pixelsPerMeter, screenVector.y / pixelsPerMeter);
	}
	
	private float getWorldDistance(float screenDistance) {
		return screenDistance / pixelsPerMeter;
	}
}
